//
//  NSObject+JMdebug.m
//  Pods
//
//  Created by Zhuangyh on 16/1/22.
//
//

#import "NSObject+JMdebug.h"
#import <objc/runtime.h>

@interface NSArray (decription)
@end
@implementation NSArray (decription)

- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *str = [NSMutableString stringWithFormat:@"%lu (\n", (unsigned long)self.count];
    
    for (id obj in self) {
        [str appendFormat:@"\t%@, \n", obj];
    }
    
    [str appendString:@")"];
    
    return str;
}
@end

@interface NSDictionary (decription)
@end
@implementation NSDictionary (decription)
- (NSString *)descriptionWithLocale:(id)locale
{
    NSArray *allKeys = [self allKeys];
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{\t\n "];
    for (NSString *key in allKeys) {
        id value= self[key];
        [str appendFormat:@"\t \"%@\" = %@,\n",key, value];
    }
    [str appendString:@"}"];
    
    return str;
}
@end

@implementation NSObject (JMdebug)

- (NSDictionary *)jm_keyValuesDic {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        id propertyValue = [self valueForKey:(NSString *)propertyName];
        if (!propertyValue){
            propertyValue = @"nil";
        }
        
        [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}

@end
